package com.honghu.villagepost.activity

import android.content.Context
import android.content.Intent
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.honghu.villagepost.adapter.GoodsAdapter
import com.honghu.villagepost.api.ApiService
import com.honghu.villagepost.base.BaseListActivity
import com.honghu.villagepost.callback.DialogCallback
import com.honghu.villagepost.callback.LzyResponse
import com.honghu.villagepost.constant.Constants
import com.honghu.villagepost.entity.Goods
import com.honghu.villagepost.util.DensityUtil
import com.honghu.villagepost.widget.decoration.GridSpacingItemDecoration
import com.lzy.okgo.model.Response
import kotlinx.android.synthetic.main.include_refresh_layout.*

/**
 * @author: mqwang
 * @create: 3/16/21 10:12 AM
 * @Description:
 */
class GoodsCidListActivity : BaseListActivity<Goods>() {

    private var cid: Int = 0
    private var shopId: Int = 0

    private val goodsAdapter : GoodsAdapter by lazy {
        GoodsAdapter()
    }

    companion object{
        fun self(context: Context, cid: Int, name: String){
            val intent = Intent(context, GoodsCidListActivity::class.java)
            intent.putExtra(Constants.CID, cid)
            intent.putExtra(Constants.NAME, name)
            context.startActivity(intent)
        }
    }

    override fun bindRecyclerView(recLv: RecyclerView?) {
        var name = intent.getStringExtra(Constants.NAME)
        setMainTitle(name)
        
        cid = intent.getIntExtra(Constants.CID, 0)

        recLv!!.layoutManager = GridLayoutManager(this, 2)
        recLv.addItemDecoration(GridSpacingItemDecoration(2, DensityUtil.dp2px(8f), true))
        recyclerView.adapter = goodsAdapter

        goodsAdapter.setOnItemClickListener { adapter, view, position ->
            var goods = adapter.getItem(position) as Goods
            GoodsDetailsActivity.self(mContext, goods.id)
        }
    }

    override fun loadData(page: Int, pageCount: Int) {
        ApiService.goods(this, shopId, cid, "", page, object : DialogCallback<LzyResponse<Goods>>(){
            override fun onSuccess(response: Response<LzyResponse<Goods>>?) {
                onGetDataSuc(response!!.body().data.records)
            }
        })
    }

    override fun getData(): MutableList<Goods> {
        return goodsAdapter.data
    }

    override fun bindData(isRefresh: Boolean, list: MutableList<Goods>?) {
        if (isRefresh){
            goodsAdapter.setNewData(list)
        }else{
            goodsAdapter.addData(list!!)
        }
    }

}